<template>
  <div class="canvas">
    <canvas id="canvas" class="verificationCode" ></canvas>
  </div>
</template>

<script lang="ts" setup>
import {defineProps,watch,onBeforeMount, reactive,onMounted} from "vue"

  const props=defineProps({
    identifyCode: { // 默认注册码
      type: String,
      default: '1234'
    },
    fontSizeMin: { // 字体最小值
      type: Number,
      default: 130
    },
    fontSizeMax: { // 字体最大值
      type: Number,
      default: 140
    }
  })
  // const d=reactive({
  //   identifyCode:"",
  //   fontSizeMin:130,
  //   fontSizeMax:140,
  // })
  // 生成一个随机数
  const randomNum =(min:number, max:number) =>{
      return Math.floor(Math.random() * (max - min) + min)
    }
   // 生成一个随机的颜色
   const randomColor= (min:number, max:number)=> {
      const r = randomNum(min, max)
      const g = randomNum(min, max)
      const b = randomNum(min, max)

      return 'rgb(' + r + ',' + g + ',' + b + ')'
    }
    const drawPic =()=> {
      
      const canvas = document.getElementById('canvas') as any
      const ctx = canvas.getContext('2d')
      ctx.textBaseline = 'bottom'
      // console.log(canvas.width)
      // 绘制背景
      ctx.fillStyle = '#e6ecfd'
      ctx.fillRect(0, 0, canvas.width, canvas.height)
      // 绘制文字
      for (let i = 0; i < props.identifyCode.length; i++) {
        drawText(ctx, props.identifyCode[i], i)
      }
      drawLine(ctx)
      drawDot(ctx)
    }
   const drawText= (ctx:any, txt:any, i:any) =>{
      const canvas = document.getElementById('canvas') as any
      ctx.fillStyle = randomColor(50, 160) // 随机生成字体颜色
      ctx.font = randomNum(props.fontSizeMin, props.fontSizeMax) + 'px SimHei' // 随机生成字体大小
      const x = (i + 0.5 ) * (canvas.width / (props.identifyCode.length + 1))
      const y = randomNum(props.fontSizeMax, canvas.height - 5)
      var deg = randomNum(-30, 30)
      // 修改坐标原点和旋转角度
      ctx.translate(x, y)
      ctx.rotate(deg * Math.PI / 180)
      ctx.fillText(txt, 0, 0)
      // 恢复坐标原点和旋转角度
      ctx.rotate(-deg * Math.PI / 180)
      ctx.translate(-x, -y)
    }

   const drawLine= (ctx:any)=> {
      // 绘制干扰线
      const canvas = document.getElementById('canvas')  as any
      for (let j = 0; j < 4; j++) {
        ctx.strokeStyle = randomColor(100, 200)
        ctx.beginPath()
        ctx.moveTo(randomNum(0, canvas.width), randomNum(0, canvas.height))
        ctx.lineTo(randomNum(0, canvas.width), randomNum(0, canvas.height))
        ctx.lineWidth = 3
        ctx.stroke()
      }
    }

   const drawDot= (ctx:any) =>{
      // 绘制干扰点
      const canvas = document.getElementById('canvas') as any
      for (let k = 0; k < 30; k++) {
        ctx.fillStyle = randomColor(0, 255)
        ctx.beginPath()
        ctx.arc(randomNum(0, canvas.width), randomNum(0, canvas.height), 3, 0, 2 * Math.PI)
        ctx.fill()
      }
    }
    watch(props,(newval,oldval)=>{
      drawPic()
    })
    // watch(props, (newval, oldval) => {
      
    // }, { immediate: true, deep: true })
    onMounted(()=>{
      
      drawPic()
      
      
    })
  

</script>

<style scoped>

  .verificationCode{
    width: 100px;
    height: 30px;
  }
</style>